﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Battleship
{
    public partial class WaterBucket : UserControl
    {
        public WaterBucket(Point location, Size size, int percent, Color waterColor, Color background, bool clickable = true)
        {
            InitializeComponent();

            this.waterColor = waterColor;
            this.percent = percent;
            this.background = background;
            this.Location = location;
            this.Size = size;

            setLabelPercent();

            Point pnLocation = new Point((this.Width - pnPercent.Width) / 2, this.Height - 30);
            this.pnPercent.Location = pnLocation;

            pnLocation = new Point((this.pnPercent.Width - this.lblPercent.Width) / 2, this.pnPercent.Height - 30);
            this.lblPercent.Location = pnLocation;
            this.clickable = clickable;
        }

        private int percent;
        private Color waterColor;
        private Color background;
        private bool clickable;

        protected override void OnPaint(PaintEventArgs e)
        {
            int height = this.Height;
            int width = this.Width;
            
            Graphics graphics = e.Graphics;

            Point point = new Point(0, height * (100 - percent) / 100);
            Size size = new Size(width, height * percent / 100);

            Brush brush = new SolidBrush(waterColor);

            Rectangle rect = new Rectangle(point, size);
            graphics.FillRectangle(brush, rect);

            brush = new SolidBrush(background);

            point = new Point(0, 0);
            size = new Size(width, height * (100 - percent) / 100);

            rect = new Rectangle(point, size);
            graphics.FillRectangle(brush, rect);
        }

        public void setPercent(int percent)
        {
            this.percent = percent;
            setLabelPercent();
            this.Refresh();
        }

        private void setLabelPercent()
        {
            String prefix = "   ";
            if (percent == 100)
            {
                prefix = "";
            }
            else if (percent > 0)
            {
                prefix = "  ";
            }

            this.lblPercent.Text = prefix + percent + "%";
        }

        public int getPercent()
        {
            return percent;
        }

        private bool isShown = false;

        private void WaterBucket_Click(object sender, EventArgs e)
        {
            if (this.clickable && !this.isShown)
            {
                this.isShown = true;
                WaterMeasurement form = new WaterMeasurement(this);
                form.Show();

                form.FormClosed += MeasurementClosed;
            }
        }

        private void MeasurementClosed(object sender, FormClosedEventArgs e)
        {
            this.isShown = false;
        }
    }
}
